﻿using System;
using System.Web.Mvc;

namespace BlueSky.Web.Extentions
{
    /// <summary>
    /// Text on a condition class.
    /// </summary>
    public static class ConditionalWriters
    {
        public static string WriteIfNotEmpty(this HtmlHelper helper, object arg, string otherwise)
        {
            return WriteIfNotEmpty(helper, arg, otherwise, "{0}");
        }

        public static string WriteIfNotEmpty(this HtmlHelper helper, object arg, string otherwise, string simpleFormat)
        {
            if (arg == null)
                throw new ArgumentNullException("arg");

            if (!string.IsNullOrEmpty(arg.ToString()))
                return string.Format(simpleFormat, arg);

            return otherwise;
        }

        /// <summary>
        /// (If value Positive) message else otherwise.
        /// </summary>
        /// <param name="helper">HtmlHelper.</param>
        /// <param name="value">Int32 value.</param>
        /// <param name="message">Message.</param>
        /// <param name="otherwise">Otherwise.</param>
        /// <returns>message or otherwise</returns>
        public static string WriteIfPositive(this HtmlHelper helper, object value, string message, string otherwise)
        {
            if (value == null)
                throw new ArgumentNullException("value");

            if (Convert.ToInt32(value) > 0)
                return message;

            return otherwise;
        }

        /// <summary>
        /// (If value NotNegative) message else otherwise.
        /// </summary>
        /// <param name="helper">HtmlHelper.</param>
        /// <param name="value">Int32 value.</param>
        /// <param name="message">Message.</param>
        /// <param name="otherwise">Otherwise.</param>
        /// <returns>message or otherwise</returns>
        public static string WriteIfNotNegative(this HtmlHelper helper, object value, string message, string otherwise)
        {
            if (value == null)
                throw new ArgumentNullException("value");

            if (Convert.ToInt32(value) >= 0)
                return message;

            return otherwise;
        }

        /// <summary>
        /// (If value Zero) message else otherwise.
        /// </summary>
        /// <param name="helper">HtmlHelper.</param>
        /// <param name="value">Int32 value.</param>
        /// <param name="message">Message.</param>
        /// <param name="otherwise">Otherwise.</param>
        /// <returns>message or otherwise</returns>
        public static string WriteIfZero(this HtmlHelper helper, object value, string message, string otherwise)
        {
            if (value == null)
                throw new ArgumentNullException("value");

            if (Convert.ToInt32(value) == 0)
                return message;

            return otherwise;
        }

        /// <summary>
        /// (If value NotZero) message else otherwise.
        /// </summary>
        /// <param name="helper">HtmlHelper.</param>
        /// <param name="value">Int32 value.</param>
        /// <param name="message">Message.</param>
        /// <param name="otherwise">Otherwise.</param>
        /// <returns>message or otherwise</returns>
        public static string WriteIfNotZero(this HtmlHelper helper, object value, string message, string otherwise)
        {
            if (value == null)
                throw new ArgumentNullException("value");

            if (Convert.ToInt32(value) != 0)
                return message;

            return otherwise;
        }

        /// <summary>
        /// (If value Negative) message else otherwise.
        /// </summary>
        /// <param name="helper">HtmlHelper.</param>
        /// <param name="value">Int32 value.</param>
        /// <param name="message">Message.</param>
        /// <param name="otherwise">Otherwise.</param>
        /// <returns>message or otherwise</returns>
        public static string WriteIfNegative(this HtmlHelper helper, object value, string message, string otherwise)
        {
            if (value == null)
                throw new ArgumentNullException("value");

            if (Convert.ToInt32(value) < 0)
                return message;

            return otherwise;
        }

        /// <summary>
        /// (If value NotPositive) message else otherwise.
        /// </summary>
        /// <param name="helper">HtmlHelper.</param>
        /// <param name="value">Int32 value.</param>
        /// <param name="message">Message.</param>
        /// <param name="otherwise">Otherwise.</param>
        /// <returns>message or otherwise</returns>
        public static string WriteIfNotPositive(this HtmlHelper helper, object value, string message, string otherwise)
        {
            if (value == null)
                throw new ArgumentNullException("value");

            if (Convert.ToInt32(value) <= 0)
                return message;

            return otherwise;
        }
    }
}
